package com.metaapp.image.view

import android.os.Bundle
import android.util.Log
import com.google.android.material.floatingactionbutton.FloatingActionButton
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.metaapp.image.adapter.ImageListAdapter
import com.metaapp.image.R
import com.metaapp.image.base.NetApiProvider
import com.metaapp.image.repo.ImageRepository
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.android.synthetic.main.content_main.view.*
import retrofit2.Retrofit

class MainActivity : AppCompatActivity() {
    val repo = ImageRepository(NetApiProvider.newImageApi())

    val imageListAdapter = ImageListAdapter(this)
    var currSearch = "北京"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val rootView = findViewById<View>(R.id.rootView)
        val searchView = findViewById<EditText>(R.id.searchView)
        findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
            currSearch = searchView.text.toString()
            refreshData(currSearch)
        }

        rootView.imageList.apply {
            layoutManager = GridLayoutManager(this@MainActivity, 2)
            adapter = imageListAdapter
        }

        imageListAdapter.setOnloadMore {
            loadData(currSearch)
        }
    }

    fun loadData(q: String = "", sn: Int = 1, pn: Int = 20) {
        repo.getImageList(q, sn, pn)
            .subscribeBy(
                onNext = {
                    imageListAdapter.addData(it)
                },
                onError = {
                    Toast.makeText(this@MainActivity, "加载失败", Toast.LENGTH_SHORT).show()
                }
            )
    }
    fun refreshData(q: String = "", sn: Int = 1, pn: Int = 20){
        repo.getImageList(q, sn, pn)
            .subscribeBy(
                onNext = {
                    imageListAdapter.setData(it)
                },
                onError = {
                    Toast.makeText(this@MainActivity, "加载失败", Toast.LENGTH_SHORT).show()
                }
            )
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
}